﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace Manager.Helper
{
    public class LoginCache
    {
        public static object lockflage = new object();
        public static bool iswork = false;
        public static readonly int errortimers = Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettings["errortimes"]);
        public static readonly int expiretime = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["expiretime"]);
        public static Dictionary<string, LoginError> login = new Dictionary<string, LoginError>();
        public static void Clear()
        {
            while (true)
            {
                try
                {
                    iswork = true;
                    var list = login.Where(p => p.Value.errortimes > errortimers && (DateTime.Now - p.Value.lastlogintime).Minutes > expiretime).ToList();
                    foreach (var l in list)
                    {
                        login.Remove(l.Key);
                    }
                }
                catch
                {
                }
                finally
                {
                    iswork = false;
                    Thread.Sleep(1000 * 60 * 2);
                }
                
            }
        }
        public static void OnLoginStart(object sender,EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(Clear));
            thread.Start();
        }
    }
    public class LoginError
    {
        public string username { get; set; }
        public int errortimes { get; set; }
        public DateTime lastlogintime { get; set; }
    }
}
